package firstping.controller;

import java.io.IOException;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.ServletException;
import javax.servlet.http.*;

import firstping.data.QnAPersistenceManager;
import firstping.model.Customer;

@SuppressWarnings("serial")
public class LoginServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/plain");
		resp.getWriter().println("Hello, world");
		
		req.setAttribute("title", "Login");
		req.setAttribute("keyword", "account,monitoring domain,firstping,statistics site");
		req.setAttribute("description", "Login.We provide web site monitoring to webmasters. Monitor your web site is availability 24 hours a day, 7 days a week with instant email alerts and weekly web site performance statistics.");
		
		try {
			req.getRequestDispatcher("/login.jsp").forward(req, resp);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/plain");
		resp.getWriter().println("Hello, world");
		
		PersistenceManager psm = QnAPersistenceManager.get().getPersistenceManager();
		
		String email = req.getParameter("email");
		String pwd = req.getParameter("pwd");

		Query query = psm.newQuery(Customer.class);
    	query.setFilter("email=='"+email+"' && password=='"+pwd+"'");
    	@SuppressWarnings("unchecked")
		List<Customer> cus = (List<Customer>) query.execute();
		
    	if(cus.size() > 0)
    	{
    		if(cus.get(0).getStatus() == 1)
    		{
		    	HttpSession session = req.getSession();
				session.setAttribute("customer", cus.get(0).getEmail());
				session.setAttribute("pwd", cus.get(0).getPassword());
				session.setAttribute("req", cus.get(0).getRequest());
				session.setAttribute("type", cus.get(0).getType());
				session.setAttribute("exp", cus.get(0).getEndDate());
				resp.sendRedirect("/account");
    		} else {
    			req.setAttribute("error", "<h3 class=\"contact\"><strong class='alert_message'>Your account is not activated.<br/> Please check your email to active the account.</strong></h3>");
        		req.setAttribute("title", "Login");
        		req.setAttribute("keyword", "account,monitoring domain,firstping,statistics site");
        		req.setAttribute("description", "Login.We provide web site monitoring to webmasters. Monitor your web site is availability 24 hours a day, 7 days a week with instant email alerts and weekly web site performance statistics.");
    			try {
    				req.getRequestDispatcher("/login.jsp").forward(req, resp);
    			} catch (ServletException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    	} else {
    		req.setAttribute("error", "<h3 class=\"contact\"><strong class='alert_message'>Wrong Username or Password</strong></h3>");
    		req.setAttribute("title", "Login");
    		req.setAttribute("keyword", "account,monitoring domain,firstping,statistics site");
    		req.setAttribute("description", "Login.We provide web site monitoring to webmasters. Monitor your web site is availability 24 hours a day, 7 days a week with instant email alerts and weekly web site performance statistics.");
			try {
				req.getRequestDispatcher("/login.jsp").forward(req, resp);
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}
	}
}
